package com.glb.leetcode;

public class C6_MaxProfit {
  public static void main(String[] args) {
    // int[] prices = {7,1,5,3,6,4};
    int[] prices = { 7, 6, 4, 3, 1, 2, 3 };
    int result = maxProfit(prices);
    System.out.println(result);
  }

  public static int maxProfit(int[] prices) {
    if (prices.length < 2) {
      return 0;
    }

    int min = prices[0];
    int max = prices[0];
    int maxProfit = 0;
    for (int i = 1; i < prices.length; i++) {
      int cur = prices[i];
      if (min > cur) {
        min = cur;
        max = cur;
      } else if (max < cur) {
        max = cur;
        int newMaxProfit = max - min;
        if (newMaxProfit > maxProfit) {
          maxProfit = newMaxProfit;
        }
      }
    }

    return maxProfit;
  }

}
